#include "U2Log.h"


U2EG_NAMESPACE_USING


//-----------------------------------------------------------------------
U2Log::U2Log( const U2String& name, bool debuggerOuput, bool suppressFile )
: mLogLevel(LL_NORMAL)
, mDebugOut(debuggerOuput)
, mSuppressFile(suppressFile)
, mTimeStamp(true)
, mLogName(name)
{
    if (!mSuppressFile)
    {
        mLog.open(name.c_str());
    }
}
//-----------------------------------------------------------------------
U2Log::~U2Log()
{
    U2_LOCK_AUTO_MUTEX
    if (!mSuppressFile)
    {
        mLog.close();
    }
}
//-----------------------------------------------------------------------
void U2Log::logMessage( const U2String& message, LogMessageLevel lml, bool maskDebug )
{
    U2_LOCK_AUTO_MUTEX
    if ((mLogLevel + lml) >= OGRE_LOG_THRESHOLD)
    {
        bool skipThisMessage = false;
        for( mtLogListener::iterator i = mListeners.begin(); i != mListeners.end(); ++i )
            (*i)->messageLogged( message, lml, maskDebug, mLogName, skipThisMessage);

        if (!skipThisMessage)
        {
            if (mDebugOut && !maskDebug)
                std::cerr << message << std::endl;

            // Write time into log
            if (!mSuppressFile)
            {
                if (mTimeStamp)
                {
                    struct tm *pTime;
                    time_t ctTime; time(&ctTime);
                    pTime = localtime( &ctTime );
                    mLog << std::setw(2) << std::setfill('0') << pTime->tm_hour
                        << ":" << std::setw(2) << std::setfill('0') << pTime->tm_min
                        << ":" << std::setw(2) << std::setfill('0') << pTime->tm_sec
                        << ": ";
                }
                mLog << message << std::endl;

                // Flush stcmdream to ensure it is written (incase of a crash, we need log to be up to date)
                mLog.flush();
            }
        }
    }
}

//-----------------------------------------------------------------------
void U2Log::setTimeStampEnabled(bool timeStamp)
{
    U2_LOCK_AUTO_MUTEX
    mTimeStamp = timeStamp;
}

//-----------------------------------------------------------------------
void U2Log::setDebugOutputEnabled(bool debugOutput)
{
    U2_LOCK_AUTO_MUTEX
    mDebugOut = debugOutput;
}

//-----------------------------------------------------------------------
void U2Log::setLogDetail(LoggingLevel ll)
{
    U2_LOCK_AUTO_MUTEX
    mLogLevel = ll;
}

//-----------------------------------------------------------------------
void U2Log::addListener(U2LogListener* listener)
{
    U2_LOCK_AUTO_MUTEX
    mListeners.push_back(listener);
}

//-----------------------------------------------------------------------
void U2Log::removeListener(U2LogListener* listener)
{
    U2_LOCK_AUTO_MUTEX
    mListeners.erase(std::find(mListeners.begin(), mListeners.end(), listener));
}
//---------------------------------------------------------------------
U2Log::Stream U2Log::stream(LogMessageLevel lml, bool maskDebug) 
{
    return Stream(this, lml, maskDebug);
}
